package com.wx.tnsperson.controller;

import com.wx.common.api.CommonResult;
import com.wx.common.api.ResultCode;
import com.wx.tnsperson.model.ProUserInfoModel;
import com.wx.tnsperson.service.impl.ProUserInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @Classname ProUserInfoController
 * @Description TODO
 * @Date 2020/6/26 19:30
 * @Created by lld
 */
@RequestMapping(value = "/userInfo")
@RestController
@Api(tags = "应聘者信息（包括在线简历；附件简历） ")
public class ProUserInfoController {

    @Autowired
    private ProUserInfoServiceImpl proUserInfoService;


    /*查询所有*/
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有应聘者信息", notes = "查询所有应聘者信息")
    @ResponseBody
    public CommonResult findAll () {
        List<ProUserInfoModel> all = proUserInfoService.findAll ( );
        if (CollectionUtils.isEmpty ( all )) {
            return CommonResult.failed ( );
        }
        return CommonResult.success ( all,"查询成功" );
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询应聘者信息", notes = "输入id 查询应聘者信息")
    @ResponseBody
    @ApiImplicitParam(name = "id", value = "根据id查询", required = true, paramType = "query", dataType = "Long")
    public ResponseEntity findById ( @ApiParam Long id ) {
        try {
            if (id != null && id == 0) {
                return ResponseEntity.badRequest ( ).build ( );
            } else {
                List<ProUserInfoModel> result = proUserInfoService.findById ( id );
                return ResponseEntity.ok ( result );
            }
        } catch (Exception e) {
            return ResponseEntity.status ( 500 ).build ( );
        }
    }

    @RequestMapping(value = "/addUserInfo", method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询应聘者信息", notes = "输入id 查询应聘者信息")
    @ResponseBody
    @ApiImplicitParam(value = "应聘者信息属性", required = true)
    public CommonResult addUserInfo ( @ApiParam ProUserInfoModel proUserInfoModel ) {
        try {
            if (isObjectFieldEmpty ( proUserInfoModel ))
                return CommonResult.validateFailed ( "请求参数不正确," );
            Integer result = proUserInfoService.addUserInfo ( proUserInfoModel );
            if (result <= 0)
                return CommonResult.failed ( ResultCode.NOT_FOUNT );
            return CommonResult.success ( );
        } catch (Exception e) {
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation(value = "修改应聘者信息", notes = "修改应聘者信息")
    @ResponseBody
    public CommonResult updateUser ( @ModelAttribute @Valid ProUserInfoModel proUserInfoModel ) {
        try {
            if (isObjectFieldEmpty ( proUserInfoModel ))
                return CommonResult.validateFailed ( "请求参数不正确," );
            Integer result = proUserInfoService.updateUser ( proUserInfoModel );
            if (result <= 0)
                return CommonResult.failed ( ResultCode.NOT_FOUNT );
            return CommonResult.success ();
        } catch (Exception e) {
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @RequestMapping(value = "/delUser", method = RequestMethod.GET)
    @ApiOperation(value = "删除应聘者信息", notes = "删除应聘者信息")
    @ResponseBody
    @ApiImplicitParam(name = "id", value = "应聘者简历信息id", required = true, paramType = "query", dataType = "Long")
    public CommonResult delUser ( @ApiParam Long id ) {
        try {
            if (id != null && id == 0)
                return CommonResult.validateFailed ( "请求参数不正确" );
            Integer result = proUserInfoService.delUser ( id );
            if (result <= 0)
                return CommonResult.failed ( ResultCode.NOT_FOUNT );
            return CommonResult.success ();
        } catch (Exception e) {
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @RequestMapping(value = "/delUpdate", method = RequestMethod.GET)
    @ApiOperation(value = "将应聘者信息放入回收站", notes = "将应聘者信息放入回收站")
    @ResponseBody
    @ApiImplicitParam(name = "id", value = "应聘者简历信息id", required = true, paramType = "query", dataType = "Long")
    public CommonResult delUpdate ( @ApiParam Long id ) {
        try {
            if (id != null && id == 0)
                return CommonResult.validateFailed ( "请求参数不正确" );
            Integer result = proUserInfoService.delUpdate ( id );
            if (result <= 0)
                return CommonResult.failed ( ResultCode.NOT_FOUNT );
            return CommonResult.success ( );
        } catch (Exception e) {
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    public boolean isObjectFieldEmpty ( Object object ) {
        boolean flag = false;
        if (object != null) {
            Class<?> entity = object.getClass ( );
            Field[] fields = entity.getDeclaredFields ( );//获取该类的所有成员变量（私有的）
            for (Field field : fields) {
                try {
                    field.setAccessible ( true );
                    if (field.getName ( ) == "id")
                        continue;
                    if (field.get ( object ) != null && !"".equals ( field.get ( object ) )) {
                        flag = true;
                        break;
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace ( );
                }
            }
        }
        return flag;
    }




}
